package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.collector.service.MessageParser;
import com.ruoyi.system.domain.GrokTest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GrokDictData;
import com.ruoyi.system.service.IGrokDictDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2023-01-01
 */
@RestController
@RequestMapping("/system/grok")
@Api(value = "Grok管理",tags = "Grok管理")
public class GrokDictDataController extends BaseController
{
    @Autowired
    private IGrokDictDataService grokDictDataService;
    @Autowired
    private MessageParser messageParser;
    /**
     * 查询【请填写功能名称】列表
     */
    @ApiOperation("查询grok")
    @PreAuthorize("@ss.hasPermi('system:grok:list')")
    @GetMapping("/list")
    public TableDataInfo list(GrokDictData grokDictData)
    {
        startPage();
        List<GrokDictData> list = grokDictDataService.selectGrokDictDataList(grokDictData);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @ApiOperation("导出grok")
    @PreAuthorize("@ss.hasPermi('system:grok:export')")
    @Log(title = "导出grok", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GrokDictData grokDictData)
    {
        List<GrokDictData> list = grokDictDataService.selectGrokDictDataList(grokDictData);
        ExcelUtil<GrokDictData> util = new ExcelUtil<GrokDictData>(GrokDictData.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取新增grok规则详细信息
     */
    @ApiOperation("获取新增grok规则详细信息")
    @PreAuthorize("@ss.hasPermi('system:grok:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(grokDictDataService.selectGrokDictDataById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @ApiOperation("新增grok规则")
    @PreAuthorize("@ss.hasPermi('system:grok:add')")
    @Log(title = "新增grok规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GrokDictData grokDictData)
    {
        return toAjax(grokDictDataService.insertGrokDictData(grokDictData));
    }

    /**
     * grok测试
     */
    @ApiOperation("grok测试")
    @PostMapping(value = "/test")
    public Map test(@RequestBody GrokTest grokTest) {
        return messageParser.testParse(grokTest.getPatterns(),grokTest.getMessage());
    }

    /**
     * 修改【请填写功能名称】
     */
    @ApiOperation("修改grok")
    @PreAuthorize("@ss.hasPermi('system:grok:edit')")
    @Log(title = "修改grok", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GrokDictData grokDictData)
    {
        return toAjax(grokDictDataService.updateGrokDictData(grokDictData));
    }

    /**
     * 删除【请填写功能名称】
     */
    @ApiOperation("删除grok")
    @PreAuthorize("@ss.hasPermi('system:grok:remove')")
    @Log(title = "删除grok", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(grokDictDataService.deleteGrokDictDataByIds(ids));
    }


}
